package com.nbooo.ui
{
	import com.somewater.controller.MenuManager;
	import com.somewater.controller.PopUpManager;
	import com.somewater.controller.RpcHandler;
	import com.somewater.display.WindowButton;
	import com.somewater.storage.Lib;
	import com.somewater.storage.RoomDef;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.ui.Mouse;
	import flash.utils.Timer;

	public class GameListScreen extends BaseMenu
	{
		/*
		 *  
		*/
		private var updateListTimer:Timer;
		private var rooms:Array;
		private var pageNum:int = 0;
		private var buttons:Array;
		private var selectedRoom:RoomDef = null;
		private var loginBtn:WindowButton;
		private const NUM_ICONS:int = 8;
		private const ICO_GAP:int = 150;
		
		private var cleared:Boolean = false;
		
		public function GameListScreen()
		{
			super("Выбор комнаты", close,buttons);
		}
		override public function clear():void
		{
			//TODO: переписать clear
			if(updateListTimer.hasEventListener(TimerEvent.TIMER))
			{
				updateListTimer.stop();
				
				updateListTimer.removeEventListener(TimerEvent.TIMER, updateGamesList)
				
			}
			updateListTimer = null;
			loginBtn.removeEventListener(MouseEvent.CLICK,onLoginClicked);
			for (var i:int = 0; i<rooms.length;i++)
			{
				rooms[i].removeEventListener(MouseEvent.CLICK, onRoomClicked);
				rooms[i].removeEventListener(MouseEvent.MOUSE_OVER, onRoomMouseOver);
				rooms[i].removeEventListener(MouseEvent.MOUSE_OVER, onRoomMouseOut);
				rooms[i] = null;
			}
			
			cleared = true;
		}
		
		override protected function init():void
		{
			super.init();
			setView();
			updateListTimer = new Timer(1000);
			updateListTimer.addEventListener(TimerEvent.TIMER, updateGamesList);
			updateListTimer.start();
			
		}
		private function setView():void
		{
			rooms = new Array();
			
			for (var i:int = 0; i < NUM_ICONS; i++)
			{
				var roomIco:RoomIcon = new RoomIcon();
				roomIco.visible = false;
				roomIco.mouseChildren = false;
				
				addChild(roomIco);
				
				if(i < 4)
				{
					roomIco.y = 100;
					roomIco.x = roomIco.width + ICO_GAP*i ;
				}
				else
				{
					roomIco.y = 300;
					roomIco.x = roomIco.width + (i-4)*ICO_GAP;
				}
				rooms.push(roomIco)
			}
				
			loginBtn = new WindowButton("Войти");
			loginBtn.addEventListener(MouseEvent.CLICK, onLoginClicked);
			loginBtn.x = (WIDTH - loginBtn.width)/2;
			loginBtn.y = HEIGHT - 50;
			addChild(loginBtn);
				
			
		}
		private function updateGamesList(e:TimerEvent):void
		{
			RpcHandler.instance.getRooms(setRooms, null);	
		}
		private function setRooms(_rooms:Array):void
		{
			if(cleared) return;
			
			var gameList:Array = _rooms;
			
			for(var i:int = 0; i < NUM_ICONS; i++ )
			{
				var k:int = i + pageNum*NUM_ICONS;
			
				if(k >= gameList.length)
					break;
			
				var room:RoomDef = gameList[k] as RoomDef;
				(rooms[i] as RoomIcon).initialize(room);
				(rooms[i] as RoomIcon).addEventListener(MouseEvent.CLICK, onRoomClicked);
				(rooms[i] as RoomIcon).addEventListener(MouseEvent.MOUSE_OVER, onRoomMouseOver);
				(rooms[i] as RoomIcon).addEventListener(MouseEvent.MOUSE_OUT, onRoomMouseOut);
				(rooms[i] as RoomIcon).visible = true;
				
			}
			i++;
			while(i< rooms.length)
			{
				rooms[i].visible = false;
				if((rooms[i] as RoomIcon).hasEventListener(MouseEvent.CLICK)){
					(rooms[i] as RoomIcon).removeEventListener(MouseEvent.CLICK, onRoomClicked);
					(rooms[i] as RoomIcon).removeEventListener(MouseEvent.MOUSE_OVER, onRoomMouseOver);
				}
				i++;
			}
		}
		private function onRoomClicked(e:MouseEvent):void
		{
			DisplayObject(e.target).filters = [new GlowFilter(0xFFFF00, 0.8)];
			
			
			try{
				selectedRoom = (e.target as RoomIcon).roomDef;
			}
			catch(e:*)
			{
				PopUpManager.message("Произошла ошибка!");
			}
			
		}
		private function onRoomMouseOver(e:MouseEvent):void
		{
			// показать инфу про комнату (?)
			if(selectedRoom == null)
				DisplayObject(e.target).filters = [new GlowFilter(0xFFFF00, 0.4)];
		}
		private function onRoomMouseOut(e:MouseEvent):void
		{
			if(selectedRoom == null)
				DisplayObject(e.target).filters = [];
		}
		private function onLoginClicked(e:MouseEvent):void
		{
			if(selectedRoom == null)
			{
				PopUpManager.message("Не выбрана ни одна комната!", "Ошибка входа");
			}else
				MenuManager.instance.showGame(selectedRoom);
		}
	}
}